﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Input;
using System.Xml;
using Catsland.Core;
using System.ComponentModel;

namespace Catsland.CatsPlugin
{
	class KeyboardInput : CatComponent
    {
        [CategoryAttribute("Input")]
        public Keys Left { set; get; }
        [CategoryAttribute("Input")]
        public Keys Right { set; get; }
        [CategoryAttribute("Input")]
        public Keys Up { set; get; }
        [CategoryAttribute("Input")]
        public Keys Down { set; get; }
        [CategoryAttribute("Input")]
        public Keys Attack { set; get; }
        [CategoryAttribute("Input")]
        public Keys Run { set; get; }
        [CategoryAttribute("Input")]
        public Keys Jump { set; get; }
        [CategoryAttribute("Input")]
        public Keys Use { set; get; }


		public KeyboardInput(GameObject gameObject)
			: base(gameObject)
		{

		}

		public override CatComponent CloneComponent(GameObject gameObject)
		{
			KeyboardInput keyboardInput = new KeyboardInput(gameObject);
			return keyboardInput;
		}

		public override void Initialize(Scene scene)
		{
			base.Initialize(scene);

            Left = Keys.A;
            Right = Keys.D;
            Up = Keys.W;
            Down = Keys.S;

            Attack = Keys.J;
            Jump = Keys.K;
            Run = Keys.L;

            Use = Keys.Space;
		}

		public override void Update(int timeLastFrame)
		{
			base.Update(timeLastFrame);

            CharacterController characterController = (CharacterController)m_gameObject
                .GetComponent(typeof(CharacterController).Name);
			if (characterController == null)
			{
				Console.WriteLine("Error! KeyboardInput Component needs CharacterController.");
				return;
			}

			KeyboardState keyboardState = Keyboard.GetState();

            characterController.m_wantLeft = keyboardState.IsKeyDown(Left);
			characterController.m_wantRight = keyboardState.IsKeyDown(Right);
			characterController.m_wantUp = keyboardState.IsKeyDown(Up);
			characterController.m_wantDown= keyboardState.IsKeyDown(Down);
			characterController.m_wantRun= keyboardState.IsKeyDown(Run);
			characterController.m_wantClimbUp = keyboardState.IsKeyDown(Keys.Q);
			characterController.m_wantClimbDown = keyboardState.IsKeyDown(Keys.E);
			characterController.m_wantJump = keyboardState.IsKeyDown(Jump);
			characterController.m_wantAttack = keyboardState.IsKeyDown(Attack);
			characterController.m_wantTalk = keyboardState.IsKeyDown(Use);

		}

		public override bool SaveToNode(XmlNode node, XmlDocument doc)
		{
			XmlElement keyboardInput = doc.CreateElement(typeof(KeyboardInput).Name);
			node.AppendChild(keyboardInput);

			return true;
		}

        public override void ConfigureFromNode(XmlElement node, Scene scene, GameObject gameObject)
        {
            base.ConfigureFromNode(node, scene, gameObject);
            return;
        }
	}
}
